var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({
  data: {
    imgUrl: globalData.imgUrl,
  },
  clickMe: function () {
    wx.canvasToTempFilePath({
      canvasId: 'testCanvas',
      fileType: 'jpg',
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            console.log(res)
            wx.showToast({title: '保存成功！'});
          },
          fail() {}
        })
      }
    })
  },
  onLoad: function (options) {
    let that = this;
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({ title: '加载中...', })
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/member/info",
      dataType: "json",
      method: "GET",
      // data: { "openId": data.openid },
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("userInfo:" + JSON.stringify(res));
        if (res.result == 0) {
          var user = res.data;
          let member_no = user.member_no;
          let posterInfo = wx.getStorageSync("posterInfo");
          let ctx = wx.createCanvasContext('testCanvas');
          let codeUrl = httpsUrl + "/wechat/getXcxCodeByPoster?scene=" + user.member_no + "," + posterInfo.poster_no + "&agentCode=" + globalData.agentCode;
          console.log("codeUrl:" + codeUrl);
          //绘制图像到画布(图片url，左侧距离，顶部距离,宽，高),顺序：z-index越小的先绘制
          // ctx.drawImage(posterInfo.img, 0, 0, 345, 445);
          // ctx.drawImage(user.img, 10, 10, 50, 50);
          // ctx.drawImage(codeUrl, 255, 355, 80, 80);
          // ctx.draw();
          console.log(posterInfo.img);
          console.log(user.img);
          //真机展示的坑：1.网络图片必须得先下载 2.图片域名必须加到downloadFile合法域名里
          wx.showLoading({ title: '加载中...', })
          wx.getImageInfo({
            src: posterInfo.img,//服务器返回的图片地址
            success: function (res) {
              wx.hideLoading();
              let Path = res.path;//res.path是网络图片的本地地址
              ctx.drawImage(Path, 0, 0, 345, 445);
              wx.getImageInfo({
                src: user.img,
                success: function (res2) {
                  let Path2 = res2.path;
                  ctx.drawImage(Path2, 8, 8, 50, 50);
                  wx.getImageInfo({
                    src: codeUrl,
                    success: function (res3) {
                      let Path3 = res3.path;
                      ctx.drawImage(Path3, 255, 355, 80, 80);
                      ctx.draw();
                    },
                  })
                },
              })
            },
            fail: function (res) {
              wx.hideLoading();
              wx.showModal({ title: '提示', content: '图片下载失败，请刷新重试！', })
            }
          })
        } else if (res.result == 2) {
          wx.showModal({title: '提示',content: '请先授权注册',})
        }
      }
    })
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})